package com.aaa.whj.controller;

import com.aaa.whj.entity.Scl;
import com.aaa.whj.service.ISclService;
import com.aaa.whj.service.impl.SclServiceImpl;
import com.mysql.jdbc.StringUtils;
import com.util.LayData;
import com.util.PrintJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/Scl")
@MultipartConfig
public class SclServlet extends HttpServlet {
    private final ISclService sclService=new SclServiceImpl();
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date(System.currentTimeMillis());
    String format = formatter.format(date);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        if(!StringUtils.isNullOrEmpty(method)&&method.equals("findAll")){
            queryAll(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("del")){
            del(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("update")){
            updateScl(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("addScl")){
            addScl(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("upload")){
            upload(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("query_scl")){
            query_scl(req,resp);
        }
    }

    private void query_scl(HttpServletRequest req, HttpServletResponse resp) {
/*        Scl scl = new Scl(req.getParameter("scl_no"),req.getParameter("scl_content"),
                format);
        List<Scl> scl1=sclService.query_scl(scl);*/
        String name = req.getParameter("name");
        System.out.println("name = " + name);
        String type = req.getParameter("type");
        System.out.println("type = " + type);
    }

    private void upload(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println(111111111);
    }

    private void addScl(HttpServletRequest req, HttpServletResponse resp) {
        Scl scl = new Scl(req.getParameter("scl_no"),req.getParameter("scl_content"),
                format);
        int i=sclService.addScl(scl);
        LayData success = LayData.success(scl);
        PrintJsonData.printData(resp,success);

    }

    private void updateScl(HttpServletRequest req, HttpServletResponse resp) {

        Scl scl = new Scl(req.getParameter("scl_id"),req.getParameter("scl_no"),req.getParameter("scl_content"),
                format);
        int i = sclService.updateScl(scl);
        LayData success = LayData.success(scl);
        PrintJsonData.printData(resp,success);


    }

    private void del(HttpServletRequest req, HttpServletResponse resp) {
        String scl_id = req.getParameter("scl_id");
        int i = sclService.deleteById(Integer.valueOf(scl_id));

    }

    private void queryAll(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        List<Scl> scl = sclService.queryAll(page, limit);
        //System.out.println("scl = " + scl);
        long count = sclService.count(null);
        //System.out.println("count = " + count);
        LayData success = LayData.success((int) count, scl);
        PrintJsonData.printData(resp,success);

    }
}
